package lib.operation;

import java.sql.*;
import java.util.Scanner;

/**
 * @author FBI
 * 2022-02-06 18:19
 */

//增 insert
public class Add implements Do{

    @Override
    public void work() {
        System.out.println("=====添加书籍=====");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入书名");
        String name = sc.nextLine();
        System.out.println("请输入书的作者");
        String author = sc.nextLine();
        System.out.println("请输入书的价格");
        double price = sc.nextDouble();
        System.out.println("请输入书的类型");
        String type = sc.next();

        Add.addBook(name,author,price,type);

    }


    public static void addBook(String name,String author,double price,String type){
        Connection conn = null;
        Statement stmt = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/lib","root","2506642408");
            stmt = conn.createStatement();
            String sql = "insert into book(name,author,price,type,isBorrowed ) values ("+ "'"+name+"'," + "'" +author+ "'," + "'" +price+ "',"+"'"+type+"',"+"'"+1+"'" + ")";
            int i = stmt.executeUpdate(sql);
            System.out.println(i == 1 ? "增加成功" : "增加失败");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
